//
//  GPBookViewController.swift
//  BLH
//
//  Created by zhangshumeng on 2021/8/11.
//

import UIKit

class GPBookViewController: BaseViewController {
    
    private var dataSource = [RecChildDataModel]()

    private lazy var tableView: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.separatorStyle = .none
        view.rowHeight = 110
        view.delegate = self
        view.dataSource = self
        view.register(cellWithClass: GPBookCell.self)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addSubviews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getData()
    }

    private func addSubviews() {
        disablesAdjustScrollViewInsets(tableView)
        
        view.addSubview(tableView)
        glt_scrollView = tableView
        
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(44)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-49)
        }
    }

}

extension GPBookViewController {
    
    private func getData() {
        dataSource = BookCacheManager.share.getAllBooks()
        tableView.reloadData()
    }
}

extension GPBookViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: GPBookCell.self)
        cell.update(dataSource[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
